<?php

// Helper method to get a string description for an HTTP status code
// From http://www.gen-x-design.com/archives/create-a-rest-api-with-php/ 
function getStatusCodeMessage($status)
{
    // these could be stored in a .ini file and loaded
    // via parse_ini_file()... however, this will suffice
    // for an example
    $codes = Array(
        100 => 'Continue',
        101 => 'Switching Protocols',
        200 => 'OK',
        201 => 'Created',
        202 => 'Accepted',
        203 => 'Non-Authoritative Information',
        204 => 'No Content',
        205 => 'Reset Content',
        206 => 'Partial Content',
        300 => 'Multiple Choices',
        301 => 'Moved Permanently',
        302 => 'Found',
        303 => 'See Other',
        304 => 'Not Modified',
        305 => 'Use Proxy',
        306 => '(Unused)',
        307 => 'Temporary Redirect',
        400 => 'Bad Request',
        401 => 'Unauthorized',
        402 => 'Payment Required',
        403 => 'Forbidden',
        404 => 'Not Found',
        405 => 'Method Not Allowed',
        406 => 'Not Acceptable',
        407 => 'Proxy Authentication Required',
        408 => 'Request Timeout',
        409 => 'Conflict',
        410 => 'Gone',
        411 => 'Length Required',
        412 => 'Precondition Failed',
        413 => 'Request Entity Too Large',
        414 => 'Request-URI Too Long',
        415 => 'Unsupported Media Type',
        416 => 'Requested Range Not Satisfiable',
        417 => 'Expectation Failed',
        500 => 'Internal Server Error',
        501 => 'Not Implemented',
        502 => 'Bad Gateway',
        503 => 'Service Unavailable',
        504 => 'Gateway Timeout',
        505 => 'HTTP Version Not Supported'
    );

    return (isset($codes[$status])) ? $codes[$status] : '';
}

// Helper method to send a HTTP response code/message
function sendResponse($status = 200, $body = '', $content_type = 'text/html')
{
    $status_header = 'HTTP/1.1 ' . $status . ' ' . getStatusCodeMessage($status);
    header($status_header);
    header('Content-type: ' . $content_type);
    echo $body;
}

class RedeemAPI {

    private $db;

    // Constructor - open DB connection
    function __construct() {
        $this->db = new mysqli('localhost', 'root', 'bir31057', 'quietplease');
        //$this->db = new mysqli('localhost', 'root', '', 'coachmouth');
        $this->db->autocommit(TRUE);
    }

    // Destructor - close DB connection
    function __destruct() {
        $this->db->close();
    }

    // Main method to redeem a code
    function redeem() {
    
        // Check for required parameters
        if (isset($_POST["user_id"]) && isset($_POST["code"])) {
        
			// Check Redemption Period
			$today = new DateTime();
			$today = $today->format('Y-m-d H:i:s');
			
			$period = new DateTime('2014-09-02 18:00:00');
			$period = $period->format('Y-m-d H:i:s');
			if($today > $period){
				 $result = array(
					"success" => 0,
					"error" => 'Redemption Period Ended',
				);
				sendResponse(200, json_encode($result));
                return false;
			}
			
            // Put parameters into local variables
            $user_id = $_POST["user_id"];
            $code = $_POST["code"];
            
            // Look up code in database
            $stmt = $this->db->prepare('SELECT id, unlocked, redeemed FROM rw_promo_code WHERE code=?');
            $stmt->bind_param("s", $code);
            $stmt->execute();
            $stmt->bind_result($id, $unlocked, $redeemed);
            while ($stmt->fetch()) {
                break;
            }
            $stmt->close();
            
            // Bail if code doesn't exist
            if ($id <= 0) {
                 $result = array(
					"success" => 0,
					"error" => 'Invalid Code',
				);
				sendResponse(200, json_encode($result));
                return false;
            }
            
            // Bail if code already used		
            if ($redeemed > 0) {
                $result = array(
					"success" => 0,
					"error" => 'Reward Already Redeemed',
				);
				sendResponse(200, json_encode($result));
                return false;
            }	
            
            
            // Add tracking of redemption
            $stmt = $this->db->prepare("INSERT INTO rw_promo_code_redeemed (rw_promo_code_id, user_id) VALUES (?, ?)");
            $stmt->bind_param("is", $id, $user_id);
            $stmt->execute();
            $stmt->close();
            
            // Increment use of code
            $this->db->query("UPDATE rw_promo_code SET redeemed=redeemed+1 WHERE id=$id");
            $this->db->commit();
            
             $result = array(
					"success" => 1,
				);
				sendResponse(200, json_encode($result));
                return false;
        }
        sendResponse(400, 'Invalid request');
        return false;
    
    }

}

// This is the first thing that gets called when this page is loaded
// Creates a new instance of the RedeemAPI class and calls the redeem method
$api = new RedeemAPI;
$api->redeem();

?>